package com.cloud.code.牛客网;

import java.util.Stack;

public class Test72 {

    public static void main(String[] args) {
        isValid("[(}]");
    }

    public static boolean isValid(String s) {
        char[] chars = s.toCharArray();

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < chars.length; i++) {
            if ('('==(chars[i]) || '{'==(chars[i]) || '['==(chars[i])) {
                stack.push(chars[i]);
            } else if (')'==(chars[i]) || '}'==(chars[i]) || ']'==(chars[i])) {
                if(stack.size() <= 0){
                    return false;
                }
                Character pop = stack.pop();
                if(')'==chars[i]){
                    if(!('('==pop)){
                        return false;
                    }
                }
                if('}'==chars[i]){
                    if(!('{'==pop)){
                        return false;
                    }
                }
                if(']'==chars[i]){
                    if(!('['==pop)){
                        return false;
                    }
                }
            }
        }

        if (stack.size() > 0) {
            return false;
        }

        return true;
        // write code here
    }
}
